package imoocvideodev.common.util.videoUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Redi
 * @Date: 2021/2/9 10:51
 * @Description: 截取视频封面的类
 */
public class FetchVideoCover {

    //ffmpeg 地址
    private static final String ffmpegEXE =  "D:/Linux/ffmpeg/ffmpeg/bin/ffmpeg.exe";


    public static void fetchCover(String videoResourcesPath,String coverOutputPath) {

        //ffmpeg.exe -ss 00:00:01 -i in.mp4 -vframes 1 out.jpg  (-vframes : 帧数设置)

        List<String> command = new ArrayList<>();
        command.add(ffmpegEXE);
        command.add("-ss");
        command.add("00:00:01");
        command.add("-i");
        command.add(videoResourcesPath);
        command.add("-vframes");
        command.add("1");
        command.add(coverOutputPath);
        commandStart(command);

    }

    public static void commandStart(List<String> command) {
        command.forEach(v -> System.out.print(v + " "));
        System.out.println();
        System.out.println();
        ProcessBuilder builder = new ProcessBuilder();
        //正常信息和错误信息合并输出
        builder.redirectErrorStream(true);
        builder.command(command);
        //开始执行命令
        Process process = null;
        try {
            process = builder.start();
            //如果你想获取到执行完后的信息，那么下面的代码也是需要的
            String line = "";
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = br.readLine()) != null) {
//                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        fetchCover("D:\\videossss\\测试用\\fire.mp4",
//                "D:\\videossss\\测试用\\out.jpg");
//    }
}
